package org.mr.disit.lessonrecorder {
	import flash.desktop.NativeProcess;
	import flash.desktop.NativeProcessStartupInfo;
	import flash.errors.IllegalOperationError;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.NativeProcessExitEvent;
	import flash.events.ProgressEvent;
	import flash.filesystem.File;
	import flash.net.Socket;
	import flash.utils.setTimeout;
	import mx.controls.Alert;
	
	public class LessonRecorder extends EventDispatcher {

		private var vlcFile:File;
		private var vlcPort:int;
		private var p:NativeProcess = new NativeProcess();
		private var startupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
		
        public function LessonRecorder() {
		}
		
        public function init(vlc:File,port:int):void {
			this.vlcFile = vlc;
			this.vlcPort = port;
			startupInfo.executable = this.vlcFile;
            
            var processArgs:Vector.<String> = new Vector.<String>();
            processArgs.push("-I")
			//processArgs.push("--extrainf")
			processArgs.push("telnet"); //Remote control
			processArgs.push("--telnet-host=localhost:" + vlcPort); //Set Telnet port
			processArgs.push("--telnet-password=air"); // Set telnet password (none)
    	    startupInfo.arguments = processArgs;
			p.addEventListener(NativeProcessExitEvent.EXIT, onExit);
            this.start();
        }
		
		public function start():void{
			if (!p.running){
				try {
					p.start(this.startupInfo);
				} catch (e:IllegalOperationError) {
					trace("Illegal Operation: " + e.toString());
				} catch (ae:ArgumentError) {
					trace("Argument Error: " + ae.toString());
				} catch (e:Error) {
					trace("Error: " + e.toString());
				}
			}
		}
		
        public function stop():void{
			if (p.running) p.exit();
		}
		
		public function kill():void{
			if (p.running) p.exit(true);
		}
		
        public function onExit(e:NativeProcessExitEvent):void {
			trace("Native Process Exit code: "+e.exitCode);
        }
	}
}